const path = require("path");
const webpack = require("webpack");
const Merge = require("webpack-merge");
const CommonConfig = require("./webpack.common.config.js");
const HtmlwebpackPlugin = require('html-webpack-plugin');
// 定义了一些文件夹的路径
const ROOT_PATH = path.resolve(__dirname);
const APP_PATH = path.resolve(ROOT_PATH, 'src');
const BUILD_PATH = path.resolve(ROOT_PATH, 'dist');
// cookie
const fs = require('fs');
// const cookie = fs.readFileSync('cookie2.txt', 'utf-8');

module.exports = Merge(CommonConfig, {
  devtool: "eval-source-map",

  entry: {
    // multiple: path.resolve(APP_PATH, 'multiple/main.js'),
    personal: path.resolve(APP_PATH, 'personal/main.js'),
    vendor: ['jquery', 'vue', 'babel-polyfill']
  },

  output: {
    path: BUILD_PATH,
    filename: '[name]_[hash:8].js'
  },

  module: {
    rules: [
      // 加载图片并置于默认文件夹下
      {
        test: /\.(png|jpg|jpeg|gif)(\?.+)?$/,
        loader: 'url-loader?name=/[path][name].[ext]'
      },
      // 加载字体并置于默认文件夹下
      {
        test: /\.(eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        loader: 'url-loader?name=/[path][name].[ext]'
      }
    ]
  },

  devServer: {
    historyApiFallback: true,
    hot: true,
    inline: true,
    // progress: true,
    openPage: 'personal.html',
    // host: 'localhost', // can be overwritten by process.env.HOST
    port: 9991, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    proxy: [
      {
        context: ["/XmlEditor", "/Materail", "/api"],
        target: "http://localhost:37511/api",
        // target: "http://192.168.107.133/coeduApi/api",
        changeOrigin: true,
        // onProxyReq(proxyReq, req, res) {
        //   // add custom header to request
        //   let cookie2;
        //   try {
        //       cookie2 = fs.readFileSync('cookie2.txt', 'utf-8');
        //       console.log(cookie2);
        //   } catch (e) {
        //       cookie2 = cookie;
        //   }
        //   if (req.get('User-Agent').indexOf('Chrome') > -1) {
        //       proxyReq.setHeader('cookie', cookie);
        //   } else {
        //       proxyReq.setHeader('cookie', cookie2);
        //   }
        // },
        onProxyRes(proxyRes, req, res) {

        },
        pathRewrite: {
          '^/api': ''
        }
      },
      {
        context: ["/corsWithHeader"],
        target: "http://192.168.107.77/ODataSearchV2/",
        changeOrigin: true,
        pathRewrite: {
          '^/corsWithHeader': ''
        }
      },
    ]
  },

  plugins: [
    // new HtmlwebpackPlugin({
    //   template: path.resolve(APP_PATH, 'multiple/index.html'),
    //   filename: 'index.html',
    //   chunks: ['runtime', 'vendor', 'multiple'],
    //   //要把script插入到标签里
    //   inject: 'body',
    //   // jshint检测代码
    //   options: {
    //     jshint: {
    //       esversion: 6
    //     },
    //   }
    // }),
    new HtmlwebpackPlugin({
      template: path.resolve(APP_PATH, 'personal/index.html'),
      filename: 'personal.html',
      chunks: ['runtime', 'vendor', 'personal'],
      //要把script插入到标签里
      inject: 'body',
      // jshint检测代码
      options: {
        jshint: {
          esversion: 6
        },
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor'
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('development')
      }
    })
  ]
})
